package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.bean.Employee;
import com.itheima.common.ParamPage;
import com.itheima.dao.EmployeeDao;
import com.itheima.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

@Service
@Transactional
public class EmployeeServiceImpl implements EmployeeService {
    @Autowired
    private EmployeeDao employeeDao;
    @Override
    public Employee findOne(Employee employee) {
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Employee::getUsername,employee.getUsername()).eq(Employee::getPassword, DigestUtils.md5DigestAsHex(employee.getPassword().getBytes()));
        return employeeDao.selectOne(lqw);
    }

    @Override
    public int add(Employee employee) {
        return employeeDao.insert(employee);
    }

    @Override
    public IPage<Employee> page(ParamPage page) {
        IPage<Employee> employeePage = new Page<>(page.getPage(), page.getPageSize());
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        lqw.like(page.getName()!=null,Employee::getName,page.getName());
        employeeDao.selectPage(employeePage,lqw);
        return employeePage;
    }

    @Override
    public int update(Employee employee) {
        return employeeDao.updateById(employee);
    }
}
